<template>
  <el-form ref="form" :model="user" :rules="rules" label-width="80px">
    <el-form-item label="旧密码" prop="oldPassword">
      <el-input v-model="user.oldPassword" placeholder="请输入旧密码" type="password" show-password />
    </el-form-item>
    <el-form-item label="新密码" prop="newPassword">
      <el-input v-model="user.newPassword" @input="checkPasswordStrength" placeholder="请输入新密码" type="password"
                show-password />
      <!-- <el-progress :percentage="passwordPercent" :format="passwordPercentFormat" :show-text="true" :text-inside="true"
        :stroke-width="15" :color="pwdColorF">
      </el-progress> -->

    </el-form-item>
    <el-form-item>
      <div class="el_input_span">
        <span class="input_span"
              :style="{ 'background-color': (passwordPercent > 30 || passwordPercent == 30) ? '#FC5F76' : '#BBBBBB', 'color': (passwordPercent > 30 || passwordPercent == 30) ? '#FC5F76' : '#BBBBBB' }">弱</span>
        <span class="input_span"
              :style="{ 'background-color': (passwordPercent > 60 || passwordPercent == 60) ? '#FF9900' : '#BBBBBB', 'color': (passwordPercent > 60 || passwordPercent == 60) ? '#FF9900' : '#BBBBBB' }">中</span>
        <span class="input_span"
              :style="{ 'background-color': (passwordPercent > 100 || passwordPercent == 100) ? '#33CC00' : '#BBBBBB', 'color': (passwordPercent > 100 || passwordPercent == 100) ? '#33CC00' : '#BBBBBB' }">强</span>
      </div>
    </el-form-item>
    <el-form-item label="确认密码" prop="confirmPassword">
      <el-input v-model="user.confirmPassword" placeholder="请确认新密码" type="password" show-password />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" size="mini" @click="submit">保存</el-button>
      <el-button type="danger" size="mini" @click="close">关闭</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import { updateUserPwd } from "@/api/system/bizuser";

export default {
  data() {
    const equalToPassword = (rule, value, callback) => {
      if (this.user.newPassword !== value) {
        callback(new Error("两次输入的密码不一致"));
      } else {
        callback();
      }
    };
    const validatePassword = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'));
        this.passwordPercent = 0;
      } else {
        //6-20位包含字符、数字和特殊字符
        var ls = 0;
        if (this.user.newPassword !== '') {
          if (this.user.newPassword.match(/([a-z])+/)) {
            ls++;
          }
          if (this.user.newPassword.match(/([0-9])+/)) {
            ls++;
          }
          if (this.user.newPassword.match(/([A-Z])+/)) {
            ls++;
          }
          if (this.user.newPassword.match(/([\W])+/) && !this.user.newPassword.match(/(![\u4E00-\u9FA5])+/)) {
            ls++;
          }
          if (this.user.newPassword.length < 8 || this.user.newPassword.length > 20) {
            callback(new Error('密码长度8位字符及以上'));
            ls = 0;
          }
          if (this.user.newPassword.match(/([\u4E00-\u9FA5])+/)) {
            callback(new Error('不能包含中文字符'));
            ls = 0;
          }
          switch (ls) {
            case 0: this.passwordPercent = 0; callback(new Error('密码长度8位及以上，必须同时包含大小写字母、数字及特殊字符至少三类')); break;
            case 1: this.passwordPercent = 30; callback(new Error('密码长度8位及以上，必须同时包含大小写字母、数字及特殊字符至少三类')); break;
            case 2: this.passwordPercent = 60; callback(new Error('密码长度8位及以上，必须同时包含大小写字母、数字及特殊字符至少三类')); break;
            case 3: this.passwordPercent = 90; break;
            case 4: this.passwordPercent = 100; break;
            default: this.passwordPercent = 0; break;
          }
        }
        callback();
      }
    };
    return {
      user: {
        oldPassword: undefined,
        newPassword: undefined,
        confirmPassword: undefined
      },
      msgText: '',
      passwordPercent: 0,
      // 表单校验
      rules: {
        oldPassword: [
          { required: true, message: "旧密码不能为空", trigger: "blur" }
        ],
        newPassword: [
          // { required: true, message: "新密码不能为空", trigger: "blur" },
          // { min: 6, max: 20, message: "长度在 6 到 20 个字符", trigger: "blur" }
          { required: true, validator: validatePassword, trigger: ['blur', 'change'] },
          { pattern: /^(?![a-zA-Z]+$)(?![A-Z0-9]+$)(?![A-Z\W_]+$)(?![a-z0-9]+$)(?![a-z\W_]+$)(?![0-9\W_]+$)[a-zA-Z0-9\W_]{8,30}$/, message: '密码为数字，小写字母，大写字母，特殊符号 至少包含三种，长度为 8 - 30位' }

        ],
        confirmPassword: [
          { required: true, message: "确认密码不能为空", trigger: "blur" },
          { required: true, validator: equalToPassword, trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    submit() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          updateUserPwd(this.user.oldPassword, this.user.newPassword).then(response => {
            this.$modal.msgSuccess("修改成功");
          });
        }
      });
    },
    close() {
      this.$tab.closePage();
    },
    checkPasswordStrength() {
      console.log('13988983----', this.passwordPercent)
      this.msgText = this.user.newPassword.length
    },
    // passwordPercentFormat(percentage) {
    //   console.log('1,', percentage)
    //   return percentage == 30 ? '弱' : percentage == 60 ? '中' : percentage == 100 ? '强' : '弱'
    // },
    // pwdColorF(percentage) {
    //   return percentage == 30 ? '#FF5340' : percentage == 60 ? '#FFB640' : percentage == 100 ? '#25DC1B' : '#FF5340'
    // },
  }
};
</script>
<style scoped>
.el_input_span {
  height: 8px;
  display: flex;
  float: left;
  width: 35%;

.input_span {
  display: inline-block;
  width: 30%;
  border-radius: 8px;
  margin-right: 3px;
  text-align: center;
  margin-top: 3px;
}
}
</style>
